<?php
session_start();
include_once("../../untils/conn.php");
mysqli_query($con, "set names utf8");

$spid = $_GET['id'];
$puid = $_GET['puid'];
$status = $_GET['status'];

// 查询数据库中是否存在对应的记录
$sql = "SELECT * FROM goodsta WHERE spid=$spid AND puid=$puid";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) == 0) {
  // 如果不存在则插入新的记录
  $sql = "INSERT INTO goodsta (spid, puid, status) VALUES ($spid, $puid, $status)";
  if (mysqli_query($con, $sql)) {
    echo "<script>alert('操作成功');window.location.href='../Goods.php';</script>";
  } else {
    echo "<script>alert('操作太快啦，请稍后刷新再试！');window.location.href='../Goods.php';</script>";
  }
} else {
  // 如果存在则更新记录
  $sql = "UPDATE goodsta SET status=$status WHERE spid=$spid AND puid=$puid";
  if (mysqli_query($con, $sql)) {
    echo "<script>alert('操作成功');window.location.href='../Goods.php';</script>";
  } else {
    echo "<script>alert('操作太快啦，请稍后刷新再试！');window.location.href='../Goods.php';</script>";
  }
}

mysqli_close($con);
?>
